<template>
  <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible" :close-on-click-modal="false">
    <el-form ref="dataForm" :rules="rules" :model="temp" label-position="left" label-width="80px" style="width: 80%; margin-left:50px;">
      <el-row :gutter="40">
        <el-col :span="12">
          <el-form-item label="任务名称" prop="jobName">
            <el-input v-model="temp.jobName" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="状态（0失败、1执行者，2成功）" prop="status" >
            <el-radio
              v-for="item in dictList('is_run')"
              :key="'type' + item.label"
              v-model="temp.status"
              :label="item.value">
              {{ item.label }}
            </el-radio>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="执行时间" prop="runDate">
            <el-date-picker v-model="temp.runDate" type="datetime" value-format="yyyy-MM-dd HH:mm" format="yyyy-MM-dd HH:mm"/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="规则名称" prop="rulesName">
            <el-input v-model="temp.rulesName" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="规则类型" prop="rulesType">
            <el-input v-model="temp.rulesType" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="任务Id" prop="jId">
            <el-input v-model="temp.jId" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="失败原因" prop="reason">
            <el-input v-model="temp.reason" type="textarea" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="dialogFormVisible = false">
        {{ $t('table.cancel') }}
      </el-button>
      <el-button type="primary" :loading="loading" @click="dialogStatus==='create'?createData():updateData()">
        {{ $t('table.confirm') }}
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
import { createmetadataJoblog, updatemetadataJoblog, getmetadataJoblog } from '@/api/log/metadataJoblog'

export default {
  name: 'metadataJoblogForm',
  data() {
    return {
      rules: {
      },
      temp: {},
      textMap: {
        update: '编辑',
        create: '新建'
      },
      loading: false,
      dialogFormVisible: false,
      dialogStatus: ''
    }
  },
  methods: {
    getList() {
      this.$emit('refreshList')
    },
    resetTemp() {
      this.loading = false
      this.temp = {
        id: undefined,
              jobName: undefined,
              status: undefined,
              createBy: undefined,
              createDate: undefined,
              updateBy: undefined,
              updateDate: undefined,
              delFlag: undefined,
              runDate: undefined,
              rulesName: undefined,
              rulesType: undefined,
              jId: undefined,
              reason: undefined
            }
    },
    handleCreate() {
      this.resetTemp()
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    createData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.loading = true
          createmetadataJoblog(this.temp).then(response => {
            this.loading = false
            if (response.data.code === 0) {
              this.getList()
              this.dialogFormVisible = false
              this.$message.success(response.data.msg)
            } else {
              this.$message.error(response.data.msg)
            }
          })
        }
      })
    },
    handleUpdate(id) {
      this.resetTemp()
      this.dialogStatus = 'update'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
      getmetadataJoblog(id).then(response => {
        if (response.data.code === 0) {
          this.temp = response.data.data
        } else {
          this.dialogFormVisible = false
          this.$message.error(response.data.msg)
        }
      })
    },
    updateData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.loading = true
          updatemetadataJoblog(this.temp).then(response => {
            this.loading = false
            if (response.data.code === 0) {
              this.dialogFormVisible = false
              this.getList()
              this.$message.success(response.data.msg)
            } else {
              this.$message.error(response.data.msg)
            }
          })
        }
      })
    }
  }
}
</script>